package com.lekang.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lekang.system.mapper.CaregiverBedBindingMapper;
import com.lekang.system.domain.CaregiverBedBinding;
import com.lekang.system.service.ICaregiverBedBindingService;

/**
 * 护理员与床位中间Service业务层处理
 *
 * @author zhihao
 * @date 2025-10-15
 */
@Service
public class CaregiverBedBindingServiceImpl implements ICaregiverBedBindingService
{
    @Autowired
    private CaregiverBedBindingMapper caregiverBedBindingMapper;

    /**
     * 查询护理员与床位中间
     *
     * @param id 护理员与床位中间主键
     * @return 护理员与床位中间
     */
    @Override
    public CaregiverBedBinding selectCaregiverBedBindingById(Long id)
    {
        return caregiverBedBindingMapper.selectCaregiverBedBindingById(id);
    }

    /**
     * 查询护理员与床位中间列表
     *
     * @param caregiverBedBinding 护理员与床位中间
     * @return 护理员与床位中间
     */
    @Override
    public List<CaregiverBedBinding> selectCaregiverBedBindingList(CaregiverBedBinding caregiverBedBinding)
    {
        return caregiverBedBindingMapper.selectCaregiverBedBindingList(caregiverBedBinding);
    }

    /**
     * 新增护理员与床位中间
     *
     * @param caregiverBedBinding 护理员与床位中间
     * @return 结果
     */
    @Override
    public int insertCaregiverBedBinding(CaregiverBedBinding caregiverBedBinding)
    {
        return caregiverBedBindingMapper.insertCaregiverBedBinding(caregiverBedBinding);
    }

    /**
     * 修改护理员与床位中间
     *
     * @param caregiverBedBinding 护理员与床位中间
     * @return 结果
     */
    @Override
    public int updateCaregiverBedBinding(CaregiverBedBinding caregiverBedBinding)
    {
        return caregiverBedBindingMapper.updateCaregiverBedBinding(caregiverBedBinding);
    }

    /**
     * 批量删除护理员与床位中间
     *
     * @param ids 需要删除的护理员与床位中间主键
     * @return 结果
     */
    @Override
    public int deleteCaregiverBedBindingByIds(Long[] ids)
    {
        return caregiverBedBindingMapper.deleteCaregiverBedBindingByIds(ids);
    }

    /**
     * 删除护理员与床位中间信息
     *
     * @param id 护理员与床位中间主键
     * @return 结果
     */
    @Override
    public int deleteCaregiverBedBindingById(Long id)
    {
        return caregiverBedBindingMapper.deleteCaregiverBedBindingById(id);
    }

    /**
     * 根据床位ID查询护理员绑定信息
     */
    @Override
    public List<CaregiverBedBinding> selectByBedId(Long bedId)
    {
        return caregiverBedBindingMapper.selectByBedId(bedId);
    }

    /**
     * 根据床位ID列表查询护理员绑定信息
     */
    @Override
    public List<CaregiverBedBinding> selectByBedIds(List<Long> bedIds)
    {
        if (bedIds == null || bedIds.isEmpty()) {
            return new ArrayList<>();
        }
        return caregiverBedBindingMapper.selectByBedIds(bedIds);
    }

    /**
     * 设置床位护理员
     */
    @Override
    @Transactional
    public boolean setBedCaregivers(Long bedId, List<Long> caregiverIds)
    {
        try {
            // 先删除该床位原有的所有护理员绑定
            caregiverBedBindingMapper.deleteByBedId(bedId);

            // 如果新的护理员列表不为空，则插入新的绑定
            if (caregiverIds != null && !caregiverIds.isEmpty()) {
                List<CaregiverBedBinding> bindings = caregiverIds.stream()
                        .map(caregiverId -> {
                            CaregiverBedBinding binding = new CaregiverBedBinding();
                            binding.setBedId(bedId);
                            binding.setCaregiverId(caregiverId);
                            return binding;
                        })
                        .collect(Collectors.toList());

                caregiverBedBindingMapper.insertBatch(bindings);
            }
            return true;
        } catch (Exception e) {
            throw new RuntimeException("设置床位护理员失败", e);
        }
    }

    /**
     * 批量设置床位护理员
     */
    @Override
    @Transactional
    public boolean batchSetBedCaregivers(List<Long> bedIds, List<Long> caregiverIds)
    {
        try {
            // 先删除这些床位原有的所有护理员绑定
            for (Long bedId : bedIds) {
                caregiverBedBindingMapper.deleteByBedId(bedId);
            }

            // 如果新的护理员列表不为空，则为每个床位插入新的绑定
            if (caregiverIds != null && !caregiverIds.isEmpty()) {
                List<CaregiverBedBinding> bindings = new ArrayList<>();
                for (Long bedId : bedIds) {
                    for (Long caregiverId : caregiverIds) {
                        CaregiverBedBinding binding = new CaregiverBedBinding();
                        binding.setBedId(bedId);
                        binding.setCaregiverId(caregiverId);
                        bindings.add(binding);
                    }
                }
                caregiverBedBindingMapper.insertBatch(bindings);
            }
            return true;
        } catch (Exception e) {
            throw new RuntimeException("批量设置床位护理员失败", e);
        }
    }

    /**
     * 移除床位的护理员
     */
    @Override
    @Transactional
    public boolean removeBedCaregiver(Long bedId, Long caregiverId)
    {
        try {
            return caregiverBedBindingMapper.deleteByBedIdAndCaregiverId(bedId, caregiverId) > 0;
        } catch (Exception e) {
            throw new RuntimeException("移除床位护理员失败", e);
        }
    }
}